<?php

	if($_GET['do']== 1){
		$pdo = new PDO('mysql:host=localhost;dbname=riris', 'root', '');
		$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		$sql = "SELECT * FROM mk";
		$stmt = $pdo->prepare($sql);
		$stmt->execute();
		$_data = $stmt->fetchAll(PDO::FETCH_OBJ);
		
		$pdo->query("USE uwika_krs_alpha");

		$sql = "INSERT INTO mk (kodemk, nama, sks)
		values (:kodemk, :nama, :sks ) ";
		$sql_u = "UPDATE  `mk` SET  `nama` = :nama WHERE  `kodemk` =  :kodemk ";
		$count = 0; $countall = $stmt->rowCount();
		$count_r = 0; $countall_ = 0;
		foreach($_data as $_data_){
			$sql2 = "SELECT * FROM mk WHERE kodemk = :kodemk";
			$stmt2 = $pdo->prepare($sql2);
			$stmt2->execute(array(':kodemk' => $_data_->KODE));
			$data3 = $stmt2->fetch(PDO::FETCH_OBJ);
			echo $_data_->KODE.' '.$_data_->NAMA;
			if( $stmt2->rowCount() <= 0){
				echo ' - inserted';
				$stmt = $pdo->prepare($sql);
				$stmt->execute(array(':kodemk' => $_data_->KODE ,':nama' => $_data_->NAMA 
									,':sks' => $_data_->SKS
				)) or die("ERROR: " . implode(":", $stmt->errorInfo()));
				$count++;
			}
			if( $data3->nama != $_data_->NAMA ){
				echo ' || '.$data3->nama.' : '.$_data_->NAMA;
				echo ' - updated';
				$stmt_u = $pdo->prepare($sql_u);
				$stmt_u->execute(array(':kodemk' => $data_->KODE ,':nama' => $_data_->NAMA 
				)) or die("ERROR: " . implode(":", $stmt_u->errorInfo()));
				$count_r++;
			}
			echo '<Br />';
			$countall_++;
		}
		echo 'data inserted : '.$count.' from '.$countall.' / '.$countall_; echo '<br />';
		echo 'data updated : '.$count_r.' from '.$countall.' / '.$countall_;
	}
	


?>